package com.xxx.topapplication;

import android.content.Context;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.Gravity;
import android.view.WindowManager;

/**
 * @author hdz
 * @date 2019/08/07
 */
public class FloatWindowManager {
    private FloatWindowView floatWindowView;
    /**
     * 大悬浮窗View的参数
     */
    private WindowManager.LayoutParams windowParams;

    /**
     * 用于控制在屏幕上添加或移除悬浮窗
     */
    private WindowManager mWindowManager;


    private volatile static FloatWindowManager instance = null;

    private FloatWindowManager() {

    }



    public static FloatWindowManager getInstance() {
        if (instance == null) {
            synchronized (FloatWindowManager.class) {
                if (instance == null) {
                    instance = new FloatWindowManager();
                }
            }
        }
        return instance;
    }


    public void setAppInfo(Drawable logo, String name, String appVer, String pkg, String apkPath) {
        if (floatWindowView != null) {
            floatWindowView.setAppLogo(logo);
            floatWindowView.setAppName(name);
            floatWindowView.setAppPkg(pkg);
            floatWindowView.setApkPath(apkPath);
            floatWindowView.setAppVer(appVer);
        }
    }

    public void createBigWindow(Context context) {
        WindowManager windowManager = getWindowManager(context);
        int screenWidth = windowManager.getDefaultDisplay().getWidth();
        int screenHeight = windowManager.getDefaultDisplay().getHeight();
        if (floatWindowView == null) {
            floatWindowView = new FloatWindowView(context);
            if (windowParams == null) {
                windowParams = new WindowManager.LayoutParams();
                windowParams.x = screenWidth / 2 - FloatWindowView.viewWidth / 2;
                windowParams.y = screenHeight / 2 - FloatWindowView.viewHeight / 2;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    windowParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
                } else {
                    windowParams.type = WindowManager.LayoutParams.TYPE_PHONE;
                }
                windowParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
                windowParams.format = PixelFormat.RGBA_8888;
                windowParams.gravity = Gravity.LEFT | Gravity.TOP;
                windowParams.width = FloatWindowView.viewWidth;
                windowParams.height = FloatWindowView.viewHeight;
            }
            floatWindowView.setParams(windowParams);
            windowManager.addView(floatWindowView, windowParams);
        }
    }


    public void removeFloatWindow(Context context) {
        if (floatWindowView != null) {
            WindowManager windowManager = getWindowManager(context);
            windowManager.removeView(floatWindowView);
            floatWindowView = null;
        }
    }


    /**
     * 如果WindowManager还未创建，则创建一个新的WindowManager返回。否则返回当前已创建的WindowManager。
     *
     * @param context 必须为应用程序的Context.
     * @return WindowManager的实例，用于控制在屏幕上添加或移除悬浮窗。
     */
    private WindowManager getWindowManager(Context context) {
        if (mWindowManager == null) {
            mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        }
        return mWindowManager;
    }
}
